package com.example.houselease.service.impl;

import com.example.houselease.ex.ServiceException;
import com.example.houselease.mapper.PictureMapper;
import com.example.houselease.pojo.dto.admin.PictureAddNewDTO;
import com.example.houselease.pojo.entity.Picture;
import com.example.houselease.service.IAdminService;
import com.example.houselease.service.IPictureService;
import com.example.houselease.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;

@Service
@Slf4j
public class IPictureServiceImpl implements IPictureService {
    @Autowired
    private PictureMapper pictureMapper;

    @Override
    public void addNew(PictureAddNewDTO pictureAddNewDTO) {
        log.debug("开始处理【添加图片】的业务，参数：{}", pictureAddNewDTO);
        int countByUrl = pictureMapper.countByUrl(pictureAddNewDTO.getUrl());
        if(countByUrl>0){
            String message = "添加图片失败，该图片已存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        Picture picture = new Picture();
        BeanUtils.copyProperties(pictureAddNewDTO,picture);
        int rows = pictureMapper.insert(picture);
        if(rows!=1){
            String message = "添加图片失败，系统忙请稍后再试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }
}
